<template>
   <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="政策标题">
                <a-input v-model="queryParam.title" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="政策分类">
				<a-select v-model="queryParam.type" placeholder="">
				  <a-select-option  v-for="(obj, index) in typeDatas" :key="index" :value="obj.name">{{obj.name}}</a-select-option>
				</a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <span class="table-page-search-submitButtons" :style="{ float: 'left', overflow: 'hidden' }">
                <a-button type="primary" @click="handleQuery">查询</a-button>
                <a-button style="margin-left: 8px" @click="handleReset">重置</a-button>
                     
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleAdd">添加政策信息</a-button>
      </div>
      <s-table
          ref="table"
          size="default"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          showPagination="auto"
		  bordered
        >
          <span slot="action" slot-scope="text, record">
             <a @click="handleEdit(record)"> <a-tag color="blue">编辑</a-tag></a>
			 <a-divider type="vertical" />
			 <a-popconfirm
			   title="是否删除该条记录?"
			   @confirm="handleDel(record)"
			   @cancel="cancel"
			   okText="是"
			   cancelText="否"
			 >
			   <a href="#"><a-tag color="blue">删除</a-tag></a>
			 </a-popconfirm>
            </span>
        </s-table>
       <policy-info-form
         ref="policyInfo"
         :title="title"
         :visible="visible"
         :loading="confirmLoading"
         :model="mdl"
         @cancel="handleCancel"
         @ok="handleOk"
       />
    </a-card>
</template>

<script>
  import { STable } from '@/components'
  import { getUrl, postAxiosUrl} from '@/api/manage'
  import PolicyInfoForm from './modules/policyinfoModal'
  export default {
    components: {
      STable,
	  PolicyInfoForm
    },
    data(){
      return {
         title:'新建',
         visible: false,
         confirmLoading: false,
         mdl: null,
		 typeDatas : [],
         queryParam: {},
         loadData: parameter => {
           const requestParameters = Object.assign({}, parameter, this.queryParam)
           return getUrl('policyInfo/list', requestParameters).then(res => {
			 console.log(res.result)
			 return res.result
           })
         }
      }
    },
    computed: {
      columns() {
        return  [
         {
           title : '政策标题',
           dataIndex: 'title'
         },
         {
            title : '政策分类',
            dataIndex: 'type'
          },
          {
            title: '创建时间',
            dataIndex: 'createDate'
          },
          {
            title: '修改时间',
            dataIndex: 'modifyDate'
          },
          {
            title: '操作',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' }
          },
        ]
      }
    },
	created () {
		getUrl('cutDictionaryDetail/getByCode', {code:'017'})
		  .then(res => {
			this.$refs.policyInfo.typedata = res.result
			this.typeDatas = res.result;
		})
	},
    methods: {
	 //  getPageData(record,params={}) {
		// getUrl('cutDictionaryDetail/getByCode', {code:'017'})
		//   .then(res => {
		// 	this.$refs.policyInfo.typedata = res.result
		// })
	 //  },
      handleQuery() {
        this.$refs.table.refresh(true)
      },
      handleReset(){
        this.queryParam = {}
        this.$refs.table.refresh(true)
      },
      handleEdit(record) {
        this.clearFields();
        this.mdl =  Object.assign(this.mdl, {...record})
		// this.getPageData(record)
        this.title =  '编辑'
        this.visible = true
      },
      clearFields () {
        this.mdl = {
          id:'',
          title: undefined,
          type: undefined,
          contents: ''
        }
      },
      handleAdd () {
		// this.getPageData(null)
        this.clearFields();
        this.title =  '新建'
        this.visible = true
      },
      handleOk (li) {
         li.$refs.liForm.validate(valid => {
          if(!valid){
             this.confirmLoading = true
          } else {
            li.$refs.liForm.clearValidate()
            postAxiosUrl('policyInfo/save',{...li.form}).then(res=>{
               this.confirmLoading = false
               this.visible = false
               this.$refs.table.refresh()
            })
          }
        })
      },
      handleCancel (li) {
        this.visible = false
        li.$refs.liForm.clearValidate()
      },
      onClose() {
        this.visible = false;
        this.$refs.policyInfo.$refs.liForm.clearValidate()
      },
	  cancel(e) {
	  },
	  handleDel(record) {
		postAxiosUrl('policyInfo/delete',{id:record.id}).then(res=>{
			 this.$refs.table.refresh()
		})
	  }
    }
  }
</script>

<style>
	.ant-select-dropdown-menu-item-selected,
	.ant-select-dropdown-menu-item-selected:hover {
	  color: #52c41a;
	  font-weight: 700;
	  background-color: #fafafa;
	}
</style>
